package com.zunipe.minix;

import com.zunipe.ZunipeTools;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.hud.InGameHud;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.HashSet;
import java.util.Set;

@Mixin(InGameHud.class)
public class InGameHudMixin
{
    @Inject(method = "renderStatusEffectOverlay", at = @At("HEAD"), cancellable = true)
    protected void renderStatusEffectOverlay(MatrixStack matrices, CallbackInfo info)
    {
        info.cancel();
    }

    @Inject(method = "render", at = @At("RETURN"))
    private void render(MatrixStack matrices, float tickDelta, CallbackInfo info)
    {
        MinecraftClient client = ZunipeTools.client;
        if (client.options.debugEnabled)
            return;
        Iterable<Entity> entities = client.world.getEntities();
        Set<String> players = new HashSet<>();
        for (Entity entity : entities)
            if (entity instanceof PlayerEntity player)
                players.add(player.getName().asString());
        int i = 0;
        boolean flag = false;

        for (String name : players)
        {
            if (name.equals(client.player.getName().asString()))
                continue;
            if (!name.matches("[a-zA-Z_0-9]+"))
                continue;
            i++;
            flag = true;
            client.textRenderer.draw(matrices, name, 5, 5 + i * 10, 0x00FA9A);
        }
        if (flag)
            client.textRenderer.draw(matrices, "附近的玩家", 5, 5, 0xFF0000);
    }
}
